﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using WebCam.Data.WebCamWorker;
//using System.Drawing;
//using WebcamCore;
//using System.Drawing.Imaging;

//namespace WebCam.Logic.Detectors
//{
//    public class ColorControllerEventArgs : WorkerComletedEventArgs
//    {
//        IEnumerable<ColorControlPoint> points;
//        public ColorControllerEventArgs(Bitmap bmp, IEnumerable<ColorControlPoint> points)
//            :base(bmp)
//        {
//            this.points = points;
//        }
//        public IEnumerable<ColorControlPoint> ControlPoints { get { return points; } }
//    }

//    public class ColorControllerWorker : IWorker
//    {        
//        private List<ColorControlPoint> colorsForControl;
//        private Rectangle rect;
//        private int delta = 25;
//        private int step = 3;

//        private Color[] viewColors = new Color[] { Color.Red, Color.Green, Color.Blue };

//        private class KlusterPoint
//        {
//            public int X { get; set; }
//            public int Y { get; set; }
//            //public int D { get; set; }
//            public int Weight { get; set; }
//            //public Color Color { get; set; }
//        }

//        //private struct ColorPos
//        //{
//        //    public long X;
//        //    public long Y;
//        //    public int Count;
//        //    public FingerTypes Type;
//        //}

//        private ColorPos[] positions;

//        public ColorControllerWorker(List<ColorControlPoint> colors)
//        {
//            colorsForControl = colors;
//            positions = new ColorPos[colorsForControl.Count];
//            for (int k = 0; k < colorsForControl.Count; k++)
//            {
//                positions[k].Type = colorsForControl[k].Type;
//            }
//        }

//        public void ProcessBitmap(Bitmap image)
//        {
//            rect = new Rectangle(0, 0, image.Width, image.Height);
//            Bitmap tmp = (Bitmap)image.Clone();

//            BitmapData imageData = image.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
//            BitmapData tempData = tmp.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

//            List<KlusterPoint>[] clusters = new List<KlusterPoint>[colorsForControl.Count];
//            for (int i = 0; i < colorsForControl.Count; i++)
//            {
//                clusters[i] = new List<KlusterPoint>();
//            }


//            unsafe 
//            {
//                for (int pointColor = 0; pointColor < colorsForControl.Count; pointColor++)
//                {                    
//                    byte* pImage = (byte*)imageData.Scan0.ToPointer();
//                    byte* pTemp = (byte*)tempData.Scan0.ToPointer();

//                    byte or = colorsForControl[pointColor].Color.R;
//                    byte og = colorsForControl[pointColor].Color.G;
//                    byte ob = colorsForControl[pointColor].Color.B;

//                    for (int j = 0; j < image.Height; j += step)
//                    {
//                        for (int i = 0; i < image.Width; i += step)
//                        {
//                            byte b = *(pImage);
//                            byte g = *(pImage + 1);
//                            byte r = *(pImage + 2);

//                            if (Math.Abs((int)r - or) < delta && Math.Abs((int)b - ob) < delta && Math.Abs((int)g - og) < delta)
//                            {
//                                if(pointColor != 0)
//                                    clusters[pointColor].Add(new KlusterPoint() { X = i, Y = j, Weight = Math.Abs(r - or) + Math.Abs(g - og) + Math.Abs(b - ob) }); 
                                
//                                *pTemp = viewColors[pointColor].B;
//                                *(pTemp + 1) = viewColors[pointColor].G;
//                                *(pTemp + 2) = viewColors[pointColor].R;
//                            }

//                            pImage += 3 * step;
//                            pTemp += 3 * step;
//                        }
//                        pImage += 3 * image.Width * (step - 1);
//                        pTemp += 3 * image.Width * (step - 1);
//                    }



//                    if (clusters[pointColor].Count != 0)
//                    {
//                        int X = (int)clusters[pointColor].Average(p => p.X);
//                        int Y = (int)clusters[pointColor].Average(p => p.Y);

//                        var CorrectPoints = clusters[pointColor].Where(p => (Math.Abs(p.X - X) < 100 && Math.Abs(p.Y - Y) < 100));

//                        if (CorrectPoints.Count() != 0)
//                        {
//                            X = (int)CorrectPoints.Average(p => p.X);
//                            Y = (int)CorrectPoints.Average(p => p.Y);

//                            colorsForControl[pointColor].Position = new Point(X, Y);
//                        }
//                    }
//                }
//            }

//            //List<
//            image.UnlockBits(imageData);
//            tmp.UnlockBits(tempData);

//            if (OnWorkerComplete != null)
//            {
//                OnWorkerComplete(this, new ColorControllerEventArgs(tmp, colorsForControl));
//            }

//        }

////        public void ProcessBitmap(Bitmap image)
////        {
////            rect = new Rectangle(0, 0, image.Width, image.Height);
////            ClearPositions();

////            List<KlusterPoint>[] clusters = new List<KlusterPoint>[colorsForControl.Count];
////            for (int i = 0; i < colorsForControl.Count; i++)
////            {
////                clusters[i] = new List<KlusterPoint>();
////            }

////            Bitmap tmp = (Bitmap)image.Clone();

////            BitmapData imageData = image.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
////            //BitmapData tmpData = tmp.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
////            unsafe
////            {
////                byte* pImage = (byte*)imageData.Scan0.ToPointer();
////                //byte* pTmpImage = (byte*)tmpData.Scan0.ToPointer();
////                for (int i = 0; i < image.Width; i++)
////                {
////                    for (int j = 0; j < image.Height; j++)
////                    {
////                        byte b = *pImage;
////                        byte g = *(pImage + 1);
////                        byte r = *(pImage + 2);

////                        for (int k = 0; k < colorsForControl.Count; k++)
////                        {
////                            byte or = colorsForControl[k].Color.R;
////                            byte og = colorsForControl[k].Color.G;
////                            byte ob = colorsForControl[k].Color.B;

////                            if (Math.Abs(r - or) < delta && Math.Abs(g - og) < delta && Math.Abs(b - ob) < delta)
////                            {
////                                clusters[k].Add(new KlusterPoint() { X = i, Y = j, Weight = Math.Abs(r - or) + Math.Abs(g - og) + Math.Abs(b - ob) });
////                                    //positions[k].X += i;
////                                    //positions[k].Y += j;
////                                    //positions[k].Count++;
////                                //*pTmpImage = 0;
////                                //*(pTmpImage + 1) = 0;
////                                //*(pTmpImage + 2) = 255;
////                            }
////                        }
////                        pImage += 3;
////                        //pTmpImage += 3;
////                    }
////                }
////            }
////            image.UnlockBits(imageData);
////            //tmp.UnlockBits(tmpData);
////            for (int k = 0; k < colorsForControl.Count; k++)
////            {
////                //clusters[k] = clusters[k].OrderBy(p => p.Weight).Take(50).ToList();

////                for (int i = 0; i < clusters[k].Count; i++)
////                {
////                    positions[k].X += clusters[k][i].X;
////                    positions[k].Y += clusters[k][i].Y;
////                    positions[k].Count++;
////                    if (k == 0)
////                    {
////                        tmp.SetPixel(clusters[k][i].X, clusters[k][i].Y, Color.Blue);
////                    }
////                    else
////                    {
////                        tmp.SetPixel(clusters[k][i].X, clusters[k][i].Y, Color.Green);
////                    }
////                }

////                if (positions[k].Count != 0)
////                {
////                    colorsForControl[k].Position = new Point((int)(positions[k].X / positions[k].Count), (int)(positions[k].Y / positions[k].Count));
////                }
////            }

////            if (OnWorkerComplete != null)
////            {
////                OnWorkerComplete(this, new ColorControllerEventArgs(tmp, colorsForControl));
//////                OnWorkerComplete(this, new ColorControllerEventArgs(image, colorsForControl));
////            }
////        }

//        private void ClearPositions()
//        {
//            for (int k = 0; k < positions.Length; k++)
//            {
//                positions[k].X = 0;
//                positions[k].Y = 0;
//                positions[k].Count = 0;
//            }
//        }

//        public event WorkerComplete OnWorkerComplete;
//    }
//}
